/**
 * Copyright (c) 2018-2099, Chill Zhuang 庄骞 (bladejava@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.modules.restaurant.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.common.tool.CommonUtil;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.restaurant.entity.ExpensesCost;
import org.springblade.modules.restaurant.mapper.ExpensesCostMapper;
import org.springblade.modules.restaurant.service.IExpensesCostService;
import org.springblade.modules.restaurant.vo.ExpensesCostVO;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 费用支出记录表 服务实现类
 *
 * @author Blade
 * @since 2025-03-03
 */
@Service
@AllArgsConstructor
public class ExpensesCostServiceImpl extends BaseServiceImpl<ExpensesCostMapper, ExpensesCost> implements IExpensesCostService {

	@Override
	public IPage<ExpensesCostVO> selectExpensesCostPage(IPage<ExpensesCostVO> page, ExpensesCostVO expensesCost) {
		return page.setRecords(baseMapper.selectExpensesCostPage(page, expensesCost));
	}

	@Override
	public BigDecimal getExpensesCostDaySum(String time, String tenantId) {

		String startTime = DateUtil.formatDate(DateUtil.beginOfMonth(DateUtil.parse(time)));
		String endTime = DateUtil.formatDate(DateUtil.endOfMonth(DateUtil.parse(time)));
		// 获取 time 月有多少天
		int day = CommonUtil.getDays(time, DatePattern.NORM_DATE_PATTERN);

		List<ExpensesCost> list = lambdaQuery().eq(ExpensesCost::getTenantId, tenantId)
			.between(ExpensesCost::getTime, startTime, endTime)
			.eq(ExpensesCost::getIsAvg, 1)
			.list();
		// 获取集合 dailyExpendAmount 的和
		BigDecimal sumExpendAmount = list.stream().map(ExpensesCost::getDailyExpendAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
		BigDecimal avgExpendAmount = sumExpendAmount.divide(new BigDecimal(day), 2, RoundingMode.HALF_UP);

		List<ExpensesCost> timeList = lambdaQuery().eq(ExpensesCost::getTenantId, tenantId)
			.eq(ExpensesCost::getIsAvg, 0)
			.eq(ExpensesCost::getTime, time)
			.list();

		if (!timeList.isEmpty()) {
			BigDecimal sum = timeList.stream().map(ExpensesCost::getDailyExpendAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
			return sum.add(avgExpendAmount);
		}
		return avgExpendAmount;
	}

	@Override
	public BigDecimal getExpensesCostMonthSum(String time, String tenantId) {
		String startTime = DateUtil.formatDate(DateUtil.beginOfMonth(DateUtil.parse(time)));
		String endTime = DateUtil.formatDate(DateUtil.endOfMonth(DateUtil.parse(time)));
		List<ExpensesCost> list = lambdaQuery().eq(ExpensesCost::getTenantId, tenantId)
			.between(ExpensesCost::getTime, startTime, endTime)
			.list();
		return list.stream().map(ExpensesCost::getDailyExpendAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
	}

}
